<?php
error_reporting(E_ALL);
include_once(dirname(__FILE__).'/app/classes/Planet.class.php');
include_once(dirname(__FILE__).'/app/lib/spyc-0.2.3/spyc.php');
require_once dirname(__FILE__).'/app/lib/lib.opml.php';
include_once(dirname(__FILE__).'/app/lib/Duration.php');
include_once(dirname(__FILE__).'/app/lib/Cache.php');
include_once(dirname(__FILE__).'/app/lib/lib.http.php');

Cache::setStore(dirname(__FILE__).'/cache/');

//Basic check
if (!is_file(dirname(__FILE__).'/custom/config.yml')){
    die('Config file (custom/config.yml) is missing.');
}

if (!is_file(dirname(__FILE__).'/custom/people.opml')){
    die('People file (custom/people.opml) is missing.');
}

//Load system files (config and OPML)
$conf = Spyc::YAMLLoad(dirname(__FILE__).'/custom/config.yml');
$opml = opmlImporter::load(dirname(__FILE__).'/custom/people.opml');

//Build config object
//Use OPML title if no title defined in the config file
$PlanetConfig = new PlanetConfig($conf);
if ($PlanetConfig->getName() === '') {
    $PlanetConfig->setName($opml->getTitle());
}

//Instantiate app
$Planet = new Planet($PlanetConfig);

//Populate app with people
$opml_people = $opml->getPeople();
foreach ($opml_people as $opml_person){	
	if (!isset($opml_person['feed'])){
		continue;
	}
	
	if (!isset($opml_person['website'])){
		//use dirname as website, doesn't work with RSS proxies like feedburner
		$opml_person['website'] = dirname($opml_person['feed']);
	}
	
	if (!isset($opml_person['name'])){
		//use url as screen name
		$opml_person['name'] = $opml_person['website'];
	}
	
    $Planet->addPerson(
        new PlanetPerson(
            $opml_person['name'],
            $opml_person['feed'],
            $opml_person['website']
        )
    );
}

//Load feeds
$Planet->loadFeeds();
$first_item_url = $Planet->items[0]->getUrl();
$first_item_date = gmdate("r", $Planet->items[0]->getDate());

//HTTP conditional get
http_modified($first_item_date, $first_item_url);

//Go display
$output_cache = $PlanetConfig->getOutputTimeout()*60;
if (array_key_exists('type', $_GET) && $_GET['type'] == 'rss10'){
	//Serve RSS 1.0
	header('Content-Type: text/xml; charset=UTF-8') ;
	if (!OutputCache::Start('rss10', $first_item_url, $output_cache)) {
		echo '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
		include_once(dirname(__FILE__).'/custom/tpl/rss10.tpl.php');
		OutputCache::End();
	}
}
elseif (array_key_exists('type', $_GET) && $_GET['type'] == 'atom10'){
	//Serve ATOM 1.0
	header('Content-Type: text/xml; charset=UTF-8') ;
	if (!OutputCache::Start('atom10', $first_item_url, $output_cache)) {
		echo '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
		include_once(dirname(__FILE__).'/custom/tpl/atom10.tpl.php');
		OutputCache::End();
	}
}
elseif (array_key_exists('type', $_GET) && $_GET['type'] == 'cheese'){
	//Serve full cache content
	header('Content-type: text/html; charset=UTF-8');
	if (!OutputCache::Start('cheese', $first_item_url, $output_cache)) {
		include_once(dirname(__FILE__).'/custom/tpl/cheese.tpl.php');
		OutputCache::End();
	}
}
else{
	//Serve regular moonmoon
	header('Content-type: text/html; charset=UTF-8');
	if (!OutputCache::Start('moonmoon', $first_item_url, $output_cache)) {
		include_once(dirname(__FILE__).'/custom/tpl/index.tpl.php');
		OutputCache::End();
	}
}
?>